import pygame
from pytmx import load_pygame
from Action.Create_sprite import create_sprite

def step8_create_sprite():
    """
    step8: 创建人物精灵
    :return:
    """
    # 初始化pygame
    pygame.init()
    # 获取游戏时钟
    clock = pygame.time.Clock()
    # 创建游戏的窗口 640 * 236 根据要显示图片的大小设置
    screen = pygame.display.set_mode((1280, 720), 0, 32)
    # 加载图片文件
    background = pygame.image.load("../Resources/tmx/map2-1.png")
    tiled_map_data = load_pygame("../Resources/tmx/map2.tmx")
    obstacle_group = pygame.sprite.Group()
    japanese_group = pygame.sprite.Group()
    prizes_group = pygame.sprite.Group()
    yu_er, captain, vice_captain = create_sprite(tiled_map_data, obstacle_group, japanese_group, prizes_group)
    while True:
        # 游戏循环
        for event in pygame.event.get():
            # 关闭事件，进行退出处理
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                yu_er.run(event.key)
        # 绘制图片到显示窗口
        screen.blit(background, (0, 0))
        yu_er.draw(screen)
        captain.draw(screen)
        vice_captain.draw(screen)
        for japanese in japanese_group:
            japanese.draw(screen)
            japanese.run()
        # 通过时钟对象指定循环频
        clock.tick(20)
        # 调用flip方法更新显示,也可以使用update方法
        pygame.display.flip()


if __name__=="__main__":
    step8_create_sprite()